package com.mall.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.kms.aliyun.exceptions.ClientException;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ShouPeng
 * @description
 * @since 2024/1/24
 */
@Component
public class SmsUtils {
    private static IAcsClient client;

    static {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G9SqrHY4kCjzxGK9djY", "3GGCfPJI3OiuM1SU5PxfMwR6Xassef");
        client =  new DefaultAcsClient(profile);
    }

    /**
     * 签名名称
     */
    private static String SIGN_NAME;
    /**
     * 模板code
     */
    private static String TEMPLATE_CODE;

    @Value("${aly.template-code}")
    public void setTemplateCode(String templateCode) {
        TEMPLATE_CODE = templateCode;
    }

    @Value("${aly.sign-name}")
    public void setSignName(String signName) {
        SIGN_NAME = signName;
    }

    /**
     * 阿里云发送验证码
     * @param phoneNumber 手机号
     * @return
     */
    public static Boolean sendSms(String phoneNumber,String randomCode){
        //构建请求
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        //以下不用更改
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //自定义参数
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);

        //创建的签名名称
        request.putQueryParameter("SignName", SIGN_NAME);

        //模板码
        request.putQueryParameter("TemplateCode", TEMPLATE_CODE);

        //构建一个短信验证码
        Map<String,Object> map = new HashMap<>(1);
        map.put("code",randomCode);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(map));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response);
            JSONObject jsonObject = JSON.parseObject(response.getData());
            String code = jsonObject.get("code") + "";
            return "OK".equals(code);
        } catch (Exception ignored) {
            System.out.println(ignored.getMessage());
        }
        return false;
    }

    public static String getIp(HttpServletRequest request){
        //负载均衡下为小写
        String ip  =  request.getHeader( "x-forward-for" );
        if (ObjectUtils.isEmpty(ip)   ||   "unknown" .equalsIgnoreCase(ip))  {
            ip  =  request.getHeader( "X-Forwarded-For" );
        }
        if (ObjectUtils.isEmpty(ip)  ||   "unknown" .equalsIgnoreCase(ip))  {
            ip  =  request.getHeader( "Proxy-Client-IP" );
        }
        if (ObjectUtils.isEmpty(ip) ||  "unknown" .equalsIgnoreCase(ip))  {
            ip  =  request.getHeader( "WL-Proxy-Client-IP" );
        }
        if (ObjectUtils.isEmpty(ip) ||  "unknown" .equalsIgnoreCase(ip))  {
            ip  =  request.getRemoteAddr();
        }
        return  ip;
    }

    /**
     * 验证手机号
     * @param phoneNumber
     * @return
     */
    public static Boolean checkPhone(String phoneNumber){
        String pattern = "0?(13|14|15|17|18|19)[0-9]{9}";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(phoneNumber);
        return m.matches();
    }


}
